<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Multiple databases &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../index.html" />
    <link rel="up" title="Models and databases" href="index.html" />
    <link rel="next" title="Tablespaces" href="tablespaces.html" />
    <link rel="prev" title="Database transactions" href="transactions.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="transactions.html" title="Database transactions">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="tablespaces.html" title="Tablespaces">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-db-multi-db">
            
  <div class="section" id="s-multiple-databases">
<span id="multiple-databases"></span><h1>Multiple databases<a class="headerlink" href="#multiple-databases" title="Permalink to this headline">¶</a></h1>
<p>This topic guide describes Django&#8217;s support for interacting with
multiple databases. Most of the rest of Django&#8217;s documentation assumes
you are interacting with a single database. If you want to interact
with multiple databases, you&#8217;ll need to take some additional steps.</p>
<div class="section" id="s-defining-your-databases">
<span id="defining-your-databases"></span><h2>Defining your databases<a class="headerlink" href="#defining-your-databases" title="Permalink to this headline">¶</a></h2>
<p>The first step to using more than one database with Django is to tell
Django about the database servers you&#8217;ll be using. This is done using
the <a class="reference internal" href="../../ref/settings.html#std:setting-DATABASES"><tt class="xref std std-setting docutils literal"><span class="pre">DATABASES</span></tt></a> setting. This setting maps database aliases,
which are a way to refer to a specific database throughout Django, to
a dictionary of settings for that specific connection. The settings in
the inner dictionaries are described fully in the <a class="reference internal" href="../../ref/settings.html#std:setting-DATABASES"><tt class="xref std std-setting docutils literal"><span class="pre">DATABASES</span></tt></a>
documentation.</p>
<p>Databases can have any alias you choose. However, the alias
<tt class="docutils literal"><span class="pre">default</span></tt> has special significance. Django uses the database with
the alias of <tt class="docutils literal"><span class="pre">default</span></tt> when no other database has been selected.</p>
<p>The following is an example <tt class="docutils literal"><span class="pre">settings.py</span></tt> snippet defining two
databases &#8211; a default PostgreSQL database and a MySQL database called
<tt class="docutils literal"><span class="pre">users</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">DATABASES</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s">&#39;default&#39;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s">&#39;NAME&#39;</span><span class="p">:</span> <span class="s">&#39;app_data&#39;</span><span class="p">,</span>
        <span class="s">&#39;ENGINE&#39;</span><span class="p">:</span> <span class="s">&#39;django.db.backends.postgresql_psycopg2&#39;</span><span class="p">,</span>
        <span class="s">&#39;USER&#39;</span><span class="p">:</span> <span class="s">&#39;postgres_user&#39;</span><span class="p">,</span>
        <span class="s">&#39;PASSWORD&#39;</span><span class="p">:</span> <span class="s">&#39;s3krit&#39;</span>
    <span class="p">},</span>
    <span class="s">&#39;users&#39;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s">&#39;NAME&#39;</span><span class="p">:</span> <span class="s">&#39;user_data&#39;</span><span class="p">,</span>
        <span class="s">&#39;ENGINE&#39;</span><span class="p">:</span> <span class="s">&#39;django.db.backends.mysql&#39;</span><span class="p">,</span>
        <span class="s">&#39;USER&#39;</span><span class="p">:</span> <span class="s">&#39;mysql_user&#39;</span><span class="p">,</span>
        <span class="s">&#39;PASSWORD&#39;</span><span class="p">:</span> <span class="s">&#39;priv4te&#39;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>If the concept of a <tt class="docutils literal"><span class="pre">default</span></tt> database doesn&#8217;t make sense in the context
of your project, you need to be careful to always specify the database
that you want to use. Django requires that a <tt class="docutils literal"><span class="pre">default</span></tt> database entry
be defined, but the parameters dictionary can be left blank if it will not be
used. You must setup <a class="reference internal" href="../../ref/settings.html#std:setting-DATABASE_ROUTERS"><tt class="xref std std-setting docutils literal"><span class="pre">DATABASE_ROUTERS</span></tt></a> for all of your apps&#8217; models,
including those in any contrib and third-party apps you are using, so that no
queries are routed to the default database in order to do this. The following
is an example <tt class="docutils literal"><span class="pre">settings.py</span></tt> snippet defining two non-default databases, with
the <tt class="docutils literal"><span class="pre">default</span></tt> entry intentionally left empty:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">DATABASES</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s">&#39;default&#39;</span><span class="p">:</span> <span class="p">{},</span>
    <span class="s">&#39;users&#39;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s">&#39;NAME&#39;</span><span class="p">:</span> <span class="s">&#39;user_data&#39;</span><span class="p">,</span>
        <span class="s">&#39;ENGINE&#39;</span><span class="p">:</span> <span class="s">&#39;django.db.backends.mysql&#39;</span><span class="p">,</span>
        <span class="s">&#39;USER&#39;</span><span class="p">:</span> <span class="s">&#39;mysql_user&#39;</span><span class="p">,</span>
        <span class="s">&#39;PASSWORD&#39;</span><span class="p">:</span> <span class="s">&#39;superS3cret&#39;</span>
    <span class="p">},</span>
    <span class="s">&#39;customers&#39;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s">&#39;NAME&#39;</span><span class="p">:</span> <span class="s">&#39;customer_data&#39;</span><span class="p">,</span>
        <span class="s">&#39;ENGINE&#39;</span><span class="p">:</span> <span class="s">&#39;django.db.backends.mysql&#39;</span><span class="p">,</span>
        <span class="s">&#39;USER&#39;</span><span class="p">:</span> <span class="s">&#39;mysql_cust&#39;</span><span class="p">,</span>
        <span class="s">&#39;PASSWORD&#39;</span><span class="p">:</span> <span class="s">&#39;veryPriv@ate&#39;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>If you attempt to access a database that you haven&#8217;t defined in your
<a class="reference internal" href="../../ref/settings.html#std:setting-DATABASES"><tt class="xref std std-setting docutils literal"><span class="pre">DATABASES</span></tt></a> setting, Django will raise a
<tt class="docutils literal"><span class="pre">django.db.utils.ConnectionDoesNotExist</span></tt> exception.</p>
</div>
<div class="section" id="s-synchronizing-your-databases">
<span id="synchronizing-your-databases"></span><h2>Synchronizing your databases<a class="headerlink" href="#synchronizing-your-databases" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="../../ref/django-admin.html#django-admin-migrate"><tt class="xref std std-djadmin docutils literal"><span class="pre">migrate</span></tt></a> management command operates on one database at a
time. By default, it operates on the <tt class="docutils literal"><span class="pre">default</span></tt> database, but by
providing a <a class="reference internal" href="../../ref/django-admin.html#django-admin-option---database"><tt class="xref std std-djadminopt docutils literal"><span class="pre">--database</span></tt></a> argument, you can tell <a class="reference internal" href="../../ref/django-admin.html#django-admin-migrate"><tt class="xref std std-djadmin docutils literal"><span class="pre">migrate</span></tt></a>
to synchronize a different database. So, to synchronize all models onto
all databases in our example, you would need to call:</p>
<div class="highlight-python"><div class="highlight"><pre>$ ./manage.py migrate
$ ./manage.py migrate --database=users
</pre></div>
</div>
<p>If you don&#8217;t want every application to be synchronized onto a
particular database, you can define a <a class="reference internal" href="#topics-db-multi-db-routing"><em>database
router</em></a> that implements a policy
constraining the availability of particular models.</p>
<div class="section" id="s-using-other-management-commands">
<span id="using-other-management-commands"></span><h3>Using other management commands<a class="headerlink" href="#using-other-management-commands" title="Permalink to this headline">¶</a></h3>
<p>The other <tt class="docutils literal"><span class="pre">django-admin.py</span></tt> commands that interact with the database
operate in the same way as <a class="reference internal" href="../../ref/django-admin.html#django-admin-migrate"><tt class="xref std std-djadmin docutils literal"><span class="pre">migrate</span></tt></a> &#8211; they only ever operate
on one database at a time, using <a class="reference internal" href="../../ref/django-admin.html#django-admin-option---database"><tt class="xref std std-djadminopt docutils literal"><span class="pre">--database</span></tt></a> to control
the database used.</p>
</div>
</div>
<div class="section" id="s-automatic-database-routing">
<span id="s-topics-db-multi-db-routing"></span><span id="automatic-database-routing"></span><span id="topics-db-multi-db-routing"></span><h2>Automatic database routing<a class="headerlink" href="#automatic-database-routing" title="Permalink to this headline">¶</a></h2>
<p>The easiest way to use multiple databases is to set up a database
routing scheme. The default routing scheme ensures that objects remain
&#8216;sticky&#8217; to their original database (i.e., an object retrieved from
the <tt class="docutils literal"><span class="pre">foo</span></tt> database will be saved on the same database). The default
routing scheme ensures that if a database isn&#8217;t specified, all queries
fall back to the <tt class="docutils literal"><span class="pre">default</span></tt> database.</p>
<p>You don&#8217;t have to do anything to activate the default routing scheme
&#8211; it is provided &#8216;out of the box&#8217; on every Django project. However,
if you want to implement more interesting database allocation
behaviors, you can define and install your own database routers.</p>
<div class="section" id="s-database-routers">
<span id="database-routers"></span><h3>Database routers<a class="headerlink" href="#database-routers" title="Permalink to this headline">¶</a></h3>
<p>A database Router is a class that provides up to four methods:</p>
<dl class="method">
<dt id="db_for_read">
<tt class="descname">db_for_read</tt>(<em>model</em>, <em>**hints</em>)<a class="headerlink" href="#db_for_read" title="Permalink to this definition">¶</a></dt>
<dd><p>Suggest the database that should be used for read operations for
objects of type <tt class="docutils literal"><span class="pre">model</span></tt>.</p>
<p>If a database operation is able to provide any additional
information that might assist in selecting a database, it will be
provided in the <tt class="docutils literal"><span class="pre">hints</span></tt> dictionary. Details on valid hints are
provided <a class="reference internal" href="#topics-db-multi-db-hints"><em>below</em></a>.</p>
<p>Returns None if there is no suggestion.</p>
</dd></dl>

<dl class="method">
<dt id="db_for_write">
<tt class="descname">db_for_write</tt>(<em>model</em>, <em>**hints</em>)<a class="headerlink" href="#db_for_write" title="Permalink to this definition">¶</a></dt>
<dd><p>Suggest the database that should be used for writes of objects of
type Model.</p>
<p>If a database operation is able to provide any additional
information that might assist in selecting a database, it will be
provided in the <tt class="docutils literal"><span class="pre">hints</span></tt> dictionary. Details on valid hints are
provided <a class="reference internal" href="#topics-db-multi-db-hints"><em>below</em></a>.</p>
<p>Returns None if there is no suggestion.</p>
</dd></dl>

<dl class="method">
<dt id="allow_relation">
<tt class="descname">allow_relation</tt>(<em>obj1</em>, <em>obj2</em>, <em>**hints</em>)<a class="headerlink" href="#allow_relation" title="Permalink to this definition">¶</a></dt>
<dd><p>Return True if a relation between obj1 and obj2 should be
allowed, False if the relation should be prevented, or None if
the router has no opinion. This is purely a validation operation,
used by foreign key and many to many operations to determine if a
relation should be allowed between two objects.</p>
</dd></dl>

<dl class="method">
<dt id="allow_migrate">
<tt class="descname">allow_migrate</tt>(<em>db</em>, <em>model</em>)<a class="headerlink" href="#allow_migrate" title="Permalink to this definition">¶</a></dt>
<dd><p>Determine if the <tt class="docutils literal"><span class="pre">model</span></tt> should have tables/indexes created in the
database with alias <tt class="docutils literal"><span class="pre">db</span></tt>. Return True if the model should be
migrated, False if it should not be migrated, or None if
the router has no opinion. This method can be used to determine
the availability of a model on a given database.</p>
<p>Note that migrations will just silently not perform any operations
on a model for which this returns <tt class="docutils literal"><span class="pre">False</span></tt>. This may result in broken
ForeignKeys, extra tables or missing tables if you change it once you
have applied some migrations.</p>
<p>The value passed for <tt class="docutils literal"><span class="pre">model</span></tt> may be a
<a class="reference internal" href="../migrations.html#historical-models"><em>historical model</em></a>, and thus not have any
custom attributes, methods or managers. You should only rely on <tt class="docutils literal"><span class="pre">_meta</span></tt>.</p>
</dd></dl>

<p>A router doesn&#8217;t have to provide <em>all</em> these methods &#8211; it may omit one
or more of them. If one of the methods is omitted, Django will skip
that router when performing the relevant check.</p>
<div class="section" id="s-hints">
<span id="s-topics-db-multi-db-hints"></span><span id="hints"></span><span id="topics-db-multi-db-hints"></span><h4>Hints<a class="headerlink" href="#hints" title="Permalink to this headline">¶</a></h4>
<p>The hints received by the database router can be used to decide which
database should receive a given request.</p>
<p>At present, the only hint that will be provided is <tt class="docutils literal"><span class="pre">instance</span></tt>, an
object instance that is related to the read or write operation that is
underway. This might be the instance that is being saved, or it might
be an instance that is being added in a many-to-many relation. In some
cases, no instance hint will be provided at all. The router checks for
the existence of an instance hint, and determine if that hint should be
used to alter routing behavior.</p>
</div>
</div>
<div class="section" id="s-using-routers">
<span id="using-routers"></span><h3>Using routers<a class="headerlink" href="#using-routers" title="Permalink to this headline">¶</a></h3>
<p>Database routers are installed using the <a class="reference internal" href="../../ref/settings.html#std:setting-DATABASE_ROUTERS"><tt class="xref std std-setting docutils literal"><span class="pre">DATABASE_ROUTERS</span></tt></a>
setting. This setting defines a list of class names, each specifying a
router that should be used by the master router
(<tt class="docutils literal"><span class="pre">django.db.router</span></tt>).</p>
<p>The master router is used by Django&#8217;s database operations to allocate
database usage. Whenever a query needs to know which database to use,
it calls the master router, providing a model and a hint (if
available). Django then tries each router in turn until a database
suggestion can be found. If no suggestion can be found, it tries the
current <tt class="docutils literal"><span class="pre">_state.db</span></tt> of the hint instance. If a hint instance wasn&#8217;t
provided, or the instance doesn&#8217;t currently have database state, the
master router will allocate the <tt class="docutils literal"><span class="pre">default</span></tt> database.</p>
</div>
<div class="section" id="s-an-example">
<span id="an-example"></span><h3>An example<a class="headerlink" href="#an-example" title="Permalink to this headline">¶</a></h3>
<div class="admonition-example-purposes-only admonition">
<p class="first admonition-title">Example purposes only!</p>
<p>This example is intended as a demonstration of how the router
infrastructure can be used to alter database usage. It
intentionally ignores some complex issues in order to
demonstrate how routers are used.</p>
<p>This example won&#8217;t work if any of the models in <tt class="docutils literal"><span class="pre">myapp</span></tt> contain
relationships to models outside of the <tt class="docutils literal"><span class="pre">other</span></tt> database.
<a class="reference internal" href="#no-cross-database-relations"><em>Cross-database relationships</em></a>
introduce referential integrity problems that Django can&#8217;t
currently handle.</p>
<p class="last">The master/slave configuration described is also flawed &#8211; it
doesn&#8217;t provide any solution for handling replication lag (i.e.,
query inconsistencies introduced because of the time taken for a
write to propagate to the slaves). It also doesn&#8217;t consider the
interaction of transactions with the database utilization strategy.</p>
</div>
<p>So - what does this mean in practice? Let&#8217;s consider another sample
configuration. This one will have several databases: one for the
<tt class="docutils literal"><span class="pre">auth</span></tt> application, and all other apps using a master/slave setup
with two read slaves. Here are the settings specifying these
databases:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">DATABASES</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s">&#39;auth_db&#39;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s">&#39;NAME&#39;</span><span class="p">:</span> <span class="s">&#39;auth_db&#39;</span><span class="p">,</span>
        <span class="s">&#39;ENGINE&#39;</span><span class="p">:</span> <span class="s">&#39;django.db.backends.mysql&#39;</span><span class="p">,</span>
        <span class="s">&#39;USER&#39;</span><span class="p">:</span> <span class="s">&#39;mysql_user&#39;</span><span class="p">,</span>
        <span class="s">&#39;PASSWORD&#39;</span><span class="p">:</span> <span class="s">&#39;swordfish&#39;</span><span class="p">,</span>
    <span class="p">},</span>
    <span class="s">&#39;master&#39;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s">&#39;NAME&#39;</span><span class="p">:</span> <span class="s">&#39;master&#39;</span><span class="p">,</span>
        <span class="s">&#39;ENGINE&#39;</span><span class="p">:</span> <span class="s">&#39;django.db.backends.mysql&#39;</span><span class="p">,</span>
        <span class="s">&#39;USER&#39;</span><span class="p">:</span> <span class="s">&#39;mysql_user&#39;</span><span class="p">,</span>
        <span class="s">&#39;PASSWORD&#39;</span><span class="p">:</span> <span class="s">&#39;spam&#39;</span><span class="p">,</span>
    <span class="p">},</span>
    <span class="s">&#39;slave1&#39;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s">&#39;NAME&#39;</span><span class="p">:</span> <span class="s">&#39;slave1&#39;</span><span class="p">,</span>
        <span class="s">&#39;ENGINE&#39;</span><span class="p">:</span> <span class="s">&#39;django.db.backends.mysql&#39;</span><span class="p">,</span>
        <span class="s">&#39;USER&#39;</span><span class="p">:</span> <span class="s">&#39;mysql_user&#39;</span><span class="p">,</span>
        <span class="s">&#39;PASSWORD&#39;</span><span class="p">:</span> <span class="s">&#39;eggs&#39;</span><span class="p">,</span>
    <span class="p">},</span>
    <span class="s">&#39;slave2&#39;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s">&#39;NAME&#39;</span><span class="p">:</span> <span class="s">&#39;slave2&#39;</span><span class="p">,</span>
        <span class="s">&#39;ENGINE&#39;</span><span class="p">:</span> <span class="s">&#39;django.db.backends.mysql&#39;</span><span class="p">,</span>
        <span class="s">&#39;USER&#39;</span><span class="p">:</span> <span class="s">&#39;mysql_user&#39;</span><span class="p">,</span>
        <span class="s">&#39;PASSWORD&#39;</span><span class="p">:</span> <span class="s">&#39;bacon&#39;</span><span class="p">,</span>
    <span class="p">},</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Now we&#8217;ll need to handle routing. First we want a router that knows to
send queries for the <tt class="docutils literal"><span class="pre">auth</span></tt> app to <tt class="docutils literal"><span class="pre">auth_db</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">AuthRouter</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A router to control all database operations on models in the</span>
<span class="sd">    auth application.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">db_for_read</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="o">**</span><span class="n">hints</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Attempts to read auth models go to auth_db.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span> <span class="o">==</span> <span class="s">&#39;auth&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="s">&#39;auth_db&#39;</span>
        <span class="k">return</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="o">**</span><span class="n">hints</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Attempts to write auth models go to auth_db.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span> <span class="o">==</span> <span class="s">&#39;auth&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="s">&#39;auth_db&#39;</span>
        <span class="k">return</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">allow_relation</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj1</span><span class="p">,</span> <span class="n">obj2</span><span class="p">,</span> <span class="o">**</span><span class="n">hints</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Allow relations if a model in the auth app is involved.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">obj1</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span> <span class="o">==</span> <span class="s">&#39;auth&#39;</span> <span class="ow">or</span> \
           <span class="n">obj2</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span> <span class="o">==</span> <span class="s">&#39;auth&#39;</span><span class="p">:</span>
           <span class="k">return</span> <span class="bp">True</span>
        <span class="k">return</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">allow_migrate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Make sure the auth app only appears in the &#39;auth_db&#39;</span>
<span class="sd">        database.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">db</span> <span class="o">==</span> <span class="s">&#39;auth_db&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span> <span class="o">==</span> <span class="s">&#39;auth&#39;</span>
        <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span> <span class="o">==</span> <span class="s">&#39;auth&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">False</span>
        <span class="k">return</span> <span class="bp">None</span>
</pre></div>
</div>
<p>And we also want a router that sends all other apps to the
master/slave configuration, and randomly chooses a slave to read
from:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">random</span>

<span class="k">class</span> <span class="nc">MasterSlaveRouter</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">db_for_read</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="o">**</span><span class="n">hints</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Reads go to a randomly-chosen slave.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">([</span><span class="s">&#39;slave1&#39;</span><span class="p">,</span> <span class="s">&#39;slave2&#39;</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="o">**</span><span class="n">hints</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Writes always go to master.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="s">&#39;master&#39;</span>

    <span class="k">def</span> <span class="nf">allow_relation</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj1</span><span class="p">,</span> <span class="n">obj2</span><span class="p">,</span> <span class="o">**</span><span class="n">hints</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Relations between objects are allowed if both objects are</span>
<span class="sd">        in the master/slave pool.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">db_list</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;master&#39;</span><span class="p">,</span> <span class="s">&#39;slave1&#39;</span><span class="p">,</span> <span class="s">&#39;slave2&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">obj1</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="ow">in</span> <span class="n">db_list</span> <span class="ow">and</span> <span class="n">obj2</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="ow">in</span> <span class="n">db_list</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">True</span>
        <span class="k">return</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">allow_migrate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        All non-auth models end up in this pool.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">True</span>
</pre></div>
</div>
<p>Finally, in the settings file, we add the following (substituting
<tt class="docutils literal"><span class="pre">path.to.</span></tt> with the actual python path to the module(s) where the
routers are defined):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">DATABASE_ROUTERS</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;path.to.AuthRouter&#39;</span><span class="p">,</span> <span class="s">&#39;path.to.MasterSlaveRouter&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>The order in which routers are processed is significant. Routers will
be queried in the order the are listed in the
<a class="reference internal" href="../../ref/settings.html#std:setting-DATABASE_ROUTERS"><tt class="xref std std-setting docutils literal"><span class="pre">DATABASE_ROUTERS</span></tt></a> setting . In this example, the
<tt class="docutils literal"><span class="pre">AuthRouter</span></tt> is processed before the <tt class="docutils literal"><span class="pre">MasterSlaveRouter</span></tt>, and as a
result, decisions concerning the models in <tt class="docutils literal"><span class="pre">auth</span></tt> are processed
before any other decision is made. If the <a class="reference internal" href="../../ref/settings.html#std:setting-DATABASE_ROUTERS"><tt class="xref std std-setting docutils literal"><span class="pre">DATABASE_ROUTERS</span></tt></a>
setting listed the two routers in the other order,
<tt class="docutils literal"><span class="pre">MasterSlaveRouter.allow_migrate()</span></tt> would be processed first. The
catch-all nature of the MasterSlaveRouter implementation would mean
that all models would be available on all databases.</p>
<p>With this setup installed, lets run some Django code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># This retrieval will be performed on the &#39;auth_db&#39; database</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fred</span> <span class="o">=</span> <span class="n">User</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">username</span><span class="o">=</span><span class="s">&#39;fred&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fred</span><span class="o">.</span><span class="n">first_name</span> <span class="o">=</span> <span class="s">&#39;Frederick&#39;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="c"># This save will also be directed to &#39;auth_db&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fred</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>

<span class="gp">&gt;&gt;&gt; </span><span class="c"># These retrieval will be randomly allocated to a slave database</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dna</span> <span class="o">=</span> <span class="n">Person</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;Douglas Adams&#39;</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="c"># A new object has no database allocation when created</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mh</span> <span class="o">=</span> <span class="n">Book</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">&#39;Mostly Harmless&#39;</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="c"># This assignment will consult the router, and set mh onto</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># the same database as the author object</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mh</span><span class="o">.</span><span class="n">author</span> <span class="o">=</span> <span class="n">dna</span>

<span class="gp">&gt;&gt;&gt; </span><span class="c"># This save will force the &#39;mh&#39; instance onto the master database...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mh</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>

<span class="gp">&gt;&gt;&gt; </span><span class="c"># ... but if we re-retrieve the object, it will come back on a slave</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mh</span> <span class="o">=</span> <span class="n">Book</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">&#39;Mostly Harmless&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-manually-selecting-a-database">
<span id="manually-selecting-a-database"></span><h2>Manually selecting a database<a class="headerlink" href="#manually-selecting-a-database" title="Permalink to this headline">¶</a></h2>
<p>Django also provides an API that allows you to maintain complete control
over database usage in your code. A manually specified database allocation
will take priority over a database allocated by a router.</p>
<div class="section" id="s-manually-selecting-a-database-for-a-queryset">
<span id="manually-selecting-a-database-for-a-queryset"></span><h3>Manually selecting a database for a <tt class="docutils literal"><span class="pre">QuerySet</span></tt><a class="headerlink" href="#manually-selecting-a-database-for-a-queryset" title="Permalink to this headline">¶</a></h3>
<p>You can select the database for a <tt class="docutils literal"><span class="pre">QuerySet</span></tt> at any point in the
<tt class="docutils literal"><span class="pre">QuerySet</span></tt> &#8220;chain.&#8221; Just call <tt class="docutils literal"><span class="pre">using()</span></tt> on the <tt class="docutils literal"><span class="pre">QuerySet</span></tt> to get
another <tt class="docutils literal"><span class="pre">QuerySet</span></tt> that uses the specified database.</p>
<p><tt class="docutils literal"><span class="pre">using()</span></tt> takes a single argument: the alias of the database on
which you want to run the query. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># This will run on the &#39;default&#39; database.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>

<span class="gp">&gt;&gt;&gt; </span><span class="c"># So will this.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="s">&#39;default&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>

<span class="gp">&gt;&gt;&gt; </span><span class="c"># This will run on the &#39;other&#39; database.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="s">&#39;other&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="s-selecting-a-database-for-save">
<span id="selecting-a-database-for-save"></span><h3>Selecting a database for <tt class="docutils literal"><span class="pre">save()</span></tt><a class="headerlink" href="#selecting-a-database-for-save" title="Permalink to this headline">¶</a></h3>
<p>Use the <tt class="docutils literal"><span class="pre">using</span></tt> keyword to <tt class="docutils literal"><span class="pre">Model.save()</span></tt> to specify to which
database the data should be saved.</p>
<p>For example, to save an object to the <tt class="docutils literal"><span class="pre">legacy_users</span></tt> database, you&#8217;d
use this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">my_object</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="s">&#39;legacy_users&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>If you don&#8217;t specify <tt class="docutils literal"><span class="pre">using</span></tt>, the <tt class="docutils literal"><span class="pre">save()</span></tt> method will save into
the default database allocated by the routers.</p>
<div class="section" id="s-moving-an-object-from-one-database-to-another">
<span id="moving-an-object-from-one-database-to-another"></span><h4>Moving an object from one database to another<a class="headerlink" href="#moving-an-object-from-one-database-to-another" title="Permalink to this headline">¶</a></h4>
<p>If you&#8217;ve saved an instance to one database, it might be tempting to
use <tt class="docutils literal"><span class="pre">save(using=...)</span></tt> as a way to migrate the instance to a new
database. However, if you don&#8217;t take appropriate steps, this could
have some unexpected consequences.</p>
<p>Consider the following example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;Fred&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="s">&#39;first&#39;</span><span class="p">)</span>  <span class="c"># (statement 1)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="s">&#39;second&#39;</span><span class="p">)</span> <span class="c"># (statement 2)</span>
</pre></div>
</div>
<p>In statement 1, a new <tt class="docutils literal"><span class="pre">Person</span></tt> object is saved to the <tt class="docutils literal"><span class="pre">first</span></tt>
database. At this time, <tt class="docutils literal"><span class="pre">p</span></tt> doesn&#8217;t have a primary key, so Django
issues an SQL <tt class="docutils literal"><span class="pre">INSERT</span></tt> statement. This creates a primary key, and
Django assigns that primary key to <tt class="docutils literal"><span class="pre">p</span></tt>.</p>
<p>When the save occurs in statement 2, <tt class="docutils literal"><span class="pre">p</span></tt> already has a primary key
value, and Django will attempt to use that primary key on the new
database. If the primary key value isn&#8217;t in use in the <tt class="docutils literal"><span class="pre">second</span></tt>
database, then you won&#8217;t have any problems &#8211; the object will be
copied to the new database.</p>
<p>However, if the primary key of <tt class="docutils literal"><span class="pre">p</span></tt> is already in use on the
<tt class="docutils literal"><span class="pre">second</span></tt> database, the existing object in the <tt class="docutils literal"><span class="pre">second</span></tt> database
will be overridden when <tt class="docutils literal"><span class="pre">p</span></tt> is saved.</p>
<p>You can avoid this in two ways. First, you can clear the primary key
of the instance. If an object has no primary key, Django will treat it
as a new object, avoiding any loss of data on the <tt class="docutils literal"><span class="pre">second</span></tt>
database:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;Fred&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="s">&#39;first&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">pk</span> <span class="o">=</span> <span class="bp">None</span> <span class="c"># Clear the primary key.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="s">&#39;second&#39;</span><span class="p">)</span> <span class="c"># Write a completely new object.</span>
</pre></div>
</div>
<p>The second option is to use the <tt class="docutils literal"><span class="pre">force_insert</span></tt> option to <tt class="docutils literal"><span class="pre">save()</span></tt>
to ensure that Django does an SQL <tt class="docutils literal"><span class="pre">INSERT</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;Fred&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="s">&#39;first&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="s">&#39;second&#39;</span><span class="p">,</span> <span class="n">force_insert</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>This will ensure that the person named <tt class="docutils literal"><span class="pre">Fred</span></tt> will have the same
primary key on both databases. If that primary key is already in use
when you try to save onto the <tt class="docutils literal"><span class="pre">second</span></tt> database, an error will be
raised.</p>
</div>
</div>
<div class="section" id="s-selecting-a-database-to-delete-from">
<span id="selecting-a-database-to-delete-from"></span><h3>Selecting a database to delete from<a class="headerlink" href="#selecting-a-database-to-delete-from" title="Permalink to this headline">¶</a></h3>
<p>By default, a call to delete an existing object will be executed on
the same database that was used to retrieve the object in the first
place:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">u</span> <span class="o">=</span> <span class="n">User</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="s">&#39;legacy_users&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">username</span><span class="o">=</span><span class="s">&#39;fred&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">u</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span> <span class="c"># will delete from the `legacy_users` database</span>
</pre></div>
</div>
<p>To specify the database from which a model will be deleted, pass a
<tt class="docutils literal"><span class="pre">using</span></tt> keyword argument to the <tt class="docutils literal"><span class="pre">Model.delete()</span></tt> method. This
argument works just like the <tt class="docutils literal"><span class="pre">using</span></tt> keyword argument to <tt class="docutils literal"><span class="pre">save()</span></tt>.</p>
<p>For example, if you&#8217;re migrating a user from the <tt class="docutils literal"><span class="pre">legacy_users</span></tt>
database to the <tt class="docutils literal"><span class="pre">new_users</span></tt> database, you might use these commands:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">user_obj</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="s">&#39;new_users&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">user_obj</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="s">&#39;legacy_users&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-using-managers-with-multiple-databases">
<span id="using-managers-with-multiple-databases"></span><h3>Using managers with multiple databases<a class="headerlink" href="#using-managers-with-multiple-databases" title="Permalink to this headline">¶</a></h3>
<p>Use the <tt class="docutils literal"><span class="pre">db_manager()</span></tt> method on managers to give managers access to
a non-default database.</p>
<p>For example, say you have a custom manager method that touches the
database &#8211; <tt class="docutils literal"><span class="pre">User.objects.create_user()</span></tt>. Because <tt class="docutils literal"><span class="pre">create_user()</span></tt>
is a manager method, not a <tt class="docutils literal"><span class="pre">QuerySet</span></tt> method, you can&#8217;t do
<tt class="docutils literal"><span class="pre">User.objects.using('new_users').create_user()</span></tt>. (The
<tt class="docutils literal"><span class="pre">create_user()</span></tt> method is only available on <tt class="docutils literal"><span class="pre">User.objects</span></tt>, the
manager, not on <tt class="docutils literal"><span class="pre">QuerySet</span></tt> objects derived from the manager.) The
solution is to use <tt class="docutils literal"><span class="pre">db_manager()</span></tt>, like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">User</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">db_manager</span><span class="p">(</span><span class="s">&#39;new_users&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">create_user</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">db_manager()</span></tt> returns a copy of the manager bound to the database you specify.</p>
<div class="section" id="s-using-get-queryset-with-multiple-databases">
<span id="using-get-queryset-with-multiple-databases"></span><h4>Using <tt class="docutils literal"><span class="pre">get_queryset()</span></tt> with multiple databases<a class="headerlink" href="#using-get-queryset-with-multiple-databases" title="Permalink to this headline">¶</a></h4>
<p>If you&#8217;re overriding <tt class="docutils literal"><span class="pre">get_queryset()</span></tt> on your manager, be sure to
either call the method on the parent (using <tt class="docutils literal"><span class="pre">super()</span></tt>) or do the
appropriate handling of the <tt class="docutils literal"><span class="pre">_db</span></tt> attribute on the manager (a string
containing the name of the database to use).</p>
<p>For example, if you want to return a custom <tt class="docutils literal"><span class="pre">QuerySet</span></tt> class from
the <tt class="docutils literal"><span class="pre">get_queryset</span></tt> method, you could do this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyManager</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Manager</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="n">CustomQuerySet</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_db</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_db</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">qs</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="section" id="s-exposing-multiple-databases-in-django-s-admin-interface">
<span id="exposing-multiple-databases-in-django-s-admin-interface"></span><h2>Exposing multiple databases in Django&#8217;s admin interface<a class="headerlink" href="#exposing-multiple-databases-in-django-s-admin-interface" title="Permalink to this headline">¶</a></h2>
<p>Django&#8217;s admin doesn&#8217;t have any explicit support for multiple
databases. If you want to provide an admin interface for a model on a
database other than that specified by your router chain, you&#8217;ll
need to write custom <a class="reference internal" href="../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><tt class="xref py py-class docutils literal"><span class="pre">ModelAdmin</span></tt></a> classes
that will direct the admin to use a specific database for content.</p>
<p><tt class="docutils literal"><span class="pre">ModelAdmin</span></tt> objects have five methods that require customization for
multiple-database support:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MultiDBModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="c"># A handy constant for the name of the alternate database.</span>
    <span class="n">using</span> <span class="o">=</span> <span class="s">&#39;other&#39;</span>

    <span class="k">def</span> <span class="nf">save_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">change</span><span class="p">):</span>
        <span class="c"># Tell Django to save objects to the &#39;other&#39; database.</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">using</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">delete_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="c"># Tell Django to delete objects from the &#39;other&#39; database</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">using</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="c"># Tell Django to look for objects on the &#39;other&#39; database.</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">MultiDBModelAdmin</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">(</span><span class="n">request</span><span class="p">)</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">using</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">formfield_for_foreignkey</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="c"># Tell Django to populate ForeignKey widgets using a query</span>
        <span class="c"># on the &#39;other&#39; database.</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">MultiDBModelAdmin</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">formfield_for_foreignkey</span><span class="p">(</span><span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="o">=</span><span class="n">request</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">using</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">formfield_for_manytomany</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="c"># Tell Django to populate ManyToMany widgets using a query</span>
        <span class="c"># on the &#39;other&#39; database.</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">MultiDBModelAdmin</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">formfield_for_manytomany</span><span class="p">(</span><span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="o">=</span><span class="n">request</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">using</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<p>The implementation provided here implements a multi-database strategy
where all objects of a given type are stored on a specific database
(e.g., all <tt class="docutils literal"><span class="pre">User</span></tt> objects are in the <tt class="docutils literal"><span class="pre">other</span></tt> database). If your
usage of multiple databases is more complex, your <tt class="docutils literal"><span class="pre">ModelAdmin</span></tt> will
need to reflect that strategy.</p>
<p>Inlines can be handled in a similar fashion. They require three customized methods:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MultiDBTabularInline</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">TabularInline</span><span class="p">):</span>
    <span class="n">using</span> <span class="o">=</span> <span class="s">&#39;other&#39;</span>

    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="c"># Tell Django to look for inline objects on the &#39;other&#39; database.</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">MultiDBTabularInline</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">(</span><span class="n">request</span><span class="p">)</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">using</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">formfield_for_foreignkey</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="c"># Tell Django to populate ForeignKey widgets using a query</span>
        <span class="c"># on the &#39;other&#39; database.</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">MultiDBTabularInline</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">formfield_for_foreignkey</span><span class="p">(</span><span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="o">=</span><span class="n">request</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">using</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">formfield_for_manytomany</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="c"># Tell Django to populate ManyToMany widgets using a query</span>
        <span class="c"># on the &#39;other&#39; database.</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">MultiDBTabularInline</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">formfield_for_manytomany</span><span class="p">(</span><span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="o">=</span><span class="n">request</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">using</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<p>Once you&#8217;ve written your model admin definitions, they can be
registered with any <tt class="docutils literal"><span class="pre">Admin</span></tt> instance:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>

<span class="c"># Specialize the multi-db admin objects for use with specific models.</span>
<span class="k">class</span> <span class="nc">BookInline</span><span class="p">(</span><span class="n">MultiDBTabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Book</span>

<span class="k">class</span> <span class="nc">PublisherAdmin</span><span class="p">(</span><span class="n">MultiDBModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">[</span><span class="n">BookInline</span><span class="p">]</span>

<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Author</span><span class="p">,</span> <span class="n">MultiDBModelAdmin</span><span class="p">)</span>
<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Publisher</span><span class="p">,</span> <span class="n">PublisherAdmin</span><span class="p">)</span>

<span class="n">othersite</span> <span class="o">=</span> <span class="n">admin</span><span class="o">.</span><span class="n">AdminSite</span><span class="p">(</span><span class="s">&#39;othersite&#39;</span><span class="p">)</span>
<span class="n">othersite</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Publisher</span><span class="p">,</span> <span class="n">MultiDBModelAdmin</span><span class="p">)</span>
</pre></div>
</div>
<p>This example sets up two admin sites. On the first site, the
<tt class="docutils literal"><span class="pre">Author</span></tt> and <tt class="docutils literal"><span class="pre">Publisher</span></tt> objects are exposed; <tt class="docutils literal"><span class="pre">Publisher</span></tt>
objects have an tabular inline showing books published by that
publisher. The second site exposes just publishers, without the
inlines.</p>
</div>
<div class="section" id="s-using-raw-cursors-with-multiple-databases">
<span id="using-raw-cursors-with-multiple-databases"></span><h2>Using raw cursors with multiple databases<a class="headerlink" href="#using-raw-cursors-with-multiple-databases" title="Permalink to this headline">¶</a></h2>
<p>If you are using more than one database you can use
<tt class="docutils literal"><span class="pre">django.db.connections</span></tt> to obtain the connection (and cursor) for a
specific database. <tt class="docutils literal"><span class="pre">django.db.connections</span></tt> is a dictionary-like
object that allows you to retrieve a specific connection using its
alias:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">connections</span>
<span class="n">cursor</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="s">&#39;my_db_alias&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="s-limitations-of-multiple-databases">
<span id="limitations-of-multiple-databases"></span><h2>Limitations of multiple databases<a class="headerlink" href="#limitations-of-multiple-databases" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-cross-database-relations">
<span id="s-no-cross-database-relations"></span><span id="cross-database-relations"></span><span id="no-cross-database-relations"></span><h3>Cross-database relations<a class="headerlink" href="#cross-database-relations" title="Permalink to this headline">¶</a></h3>
<p>Django doesn&#8217;t currently provide any support for foreign key or
many-to-many relationships spanning multiple databases. If you
have used a router to partition models to different databases,
any foreign key and many-to-many relationships defined by those
models must be internal to a single database.</p>
<p>This is because of referential integrity. In order to maintain a
relationship between two objects, Django needs to know that the
primary key of the related object is valid. If the primary key is
stored on a separate database, it&#8217;s not possible to easily evaluate
the validity of a primary key.</p>
<p>If you&#8217;re using Postgres, Oracle, or MySQL with InnoDB, this is
enforced at the database integrity level &#8211; database level key
constraints prevent the creation of relations that can&#8217;t be validated.</p>
<p>However, if you&#8217;re using SQLite or MySQL with MyISAM tables, there is
no enforced referential integrity; as a result, you may be able to
&#8216;fake&#8217; cross database foreign keys. However, this configuration is not
officially supported by Django.</p>
</div>
<div class="section" id="s-behavior-of-contrib-apps">
<span id="s-contrib-app-multiple-databases"></span><span id="behavior-of-contrib-apps"></span><span id="contrib-app-multiple-databases"></span><h3>Behavior of contrib apps<a class="headerlink" href="#behavior-of-contrib-apps" title="Permalink to this headline">¶</a></h3>
<p>Several contrib apps include models, and some apps depend on others. Since
cross-database relationships are impossible, this creates some restrictions on
how you can split these models across databases:</p>
<ul class="simple">
<li>each one of <tt class="docutils literal"><span class="pre">contenttypes.ContentType</span></tt>, <tt class="docutils literal"><span class="pre">sessions.Session</span></tt> and
<tt class="docutils literal"><span class="pre">sites.Site</span></tt> can be stored in any database, given a suitable router.</li>
<li><tt class="docutils literal"><span class="pre">auth</span></tt> models — <tt class="docutils literal"><span class="pre">User</span></tt>, <tt class="docutils literal"><span class="pre">Group</span></tt> and <tt class="docutils literal"><span class="pre">Permission</span></tt> — are linked
together and linked to <tt class="docutils literal"><span class="pre">ContentType</span></tt>, so they must be stored in the same
database as <tt class="docutils literal"><span class="pre">ContentType</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">admin</span></tt> and <tt class="docutils literal"><span class="pre">comments</span></tt> depend on <tt class="docutils literal"><span class="pre">auth</span></tt>, so their models must be in
the same database as <tt class="docutils literal"><span class="pre">auth</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">flatpages</span></tt> and <tt class="docutils literal"><span class="pre">redirects</span></tt> depend on <tt class="docutils literal"><span class="pre">sites</span></tt>, so their models must be
in the same database as <tt class="docutils literal"><span class="pre">sites</span></tt>.</li>
</ul>
<p>In addition, some objects are automatically created just after
<a class="reference internal" href="../../ref/django-admin.html#django-admin-migrate"><tt class="xref std std-djadmin docutils literal"><span class="pre">migrate</span></tt></a> creates a table to hold them in a database:</p>
<ul class="simple">
<li>a default <tt class="docutils literal"><span class="pre">Site</span></tt>,</li>
<li>a <tt class="docutils literal"><span class="pre">ContentType</span></tt> for each model (including those not stored in that
database),</li>
<li>three <tt class="docutils literal"><span class="pre">Permission</span></tt> for each model (including those not stored in that
database).</li>
</ul>
<p>For common setups with multiple databases, it isn&#8217;t useful to have these
objects in more than one database. Common setups include primary/replica and
connecting to external databases. Therefore, it&#8217;s recommended to write a
<a class="reference internal" href="#topics-db-multi-db-routing"><em>database router</em></a> that allows synchronizing
these three models to only one database. Use the same approach for contrib
and third-party apps that don&#8217;t need their tables in multiple databases.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">If you&#8217;re synchronizing content types to more than one database, be aware
that their primary keys may not match across databases. This may result in
data corruption or data loss.</p>
</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Multiple databases</a><ul>
<li><a class="reference internal" href="#defining-your-databases">Defining your databases</a></li>
<li><a class="reference internal" href="#synchronizing-your-databases">Synchronizing your databases</a><ul>
<li><a class="reference internal" href="#using-other-management-commands">Using other management commands</a></li>
</ul>
</li>
<li><a class="reference internal" href="#automatic-database-routing">Automatic database routing</a><ul>
<li><a class="reference internal" href="#database-routers">Database routers</a><ul>
<li><a class="reference internal" href="#hints">Hints</a></li>
</ul>
</li>
<li><a class="reference internal" href="#using-routers">Using routers</a></li>
<li><a class="reference internal" href="#an-example">An example</a></li>
</ul>
</li>
<li><a class="reference internal" href="#manually-selecting-a-database">Manually selecting a database</a><ul>
<li><a class="reference internal" href="#manually-selecting-a-database-for-a-queryset">Manually selecting a database for a <tt class="docutils literal"><span class="pre">QuerySet</span></tt></a></li>
<li><a class="reference internal" href="#selecting-a-database-for-save">Selecting a database for <tt class="docutils literal"><span class="pre">save()</span></tt></a><ul>
<li><a class="reference internal" href="#moving-an-object-from-one-database-to-another">Moving an object from one database to another</a></li>
</ul>
</li>
<li><a class="reference internal" href="#selecting-a-database-to-delete-from">Selecting a database to delete from</a></li>
<li><a class="reference internal" href="#using-managers-with-multiple-databases">Using managers with multiple databases</a><ul>
<li><a class="reference internal" href="#using-get-queryset-with-multiple-databases">Using <tt class="docutils literal"><span class="pre">get_queryset()</span></tt> with multiple databases</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#exposing-multiple-databases-in-django-s-admin-interface">Exposing multiple databases in Django&#8217;s admin interface</a></li>
<li><a class="reference internal" href="#using-raw-cursors-with-multiple-databases">Using raw cursors with multiple databases</a></li>
<li><a class="reference internal" href="#limitations-of-multiple-databases">Limitations of multiple databases</a><ul>
<li><a class="reference internal" href="#cross-database-relations">Cross-database relations</a></li>
<li><a class="reference internal" href="#behavior-of-contrib-apps">Behavior of contrib apps</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="transactions.html">Database transactions</a></li>
    
    
      <li>Next: <a href="tablespaces.html">Tablespaces</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../index.html">Using Django</a>
        
          <ul><li><a href="index.html">Models and databases</a>
        
        <ul><li>Multiple databases</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/topics/db/multi-db.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="transactions.html" title="Database transactions">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="tablespaces.html" title="Tablespaces">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>